"""
 -*- coding: utf-8-*-
 @File: conftest.py
 @Author: chen
 @time: 2023/07/08/ 13:22
"""
from typing import List

import pytest
import yaml


# 解决用例描述中中文乱码的问题
def pytest_collection_modifyitems(session: "Session", config: "Config", items: List["Item"]) -> None:
    for item in items:
        item.product_name = item.product_name.encode('utf-8').decode('unicode-escape')
        item._nodeid = item.nodeid.encode('utf-8').decode('unicode-escape')


# 获取测试执行FIAL的信息
@pytest.hookimpl(hookwrapper=True, tryfirst=True)
def pytest_runtest_makereport(item, call):
    # 获取勾子方法的执行结果
    out = yield
    print(f"用例执行结果：{out}")
    report = out.get_result()
    print(f"测试报告：{report}")
    failed_case_list = []
    if report.outcome != "passed":
        failed_case_info = {
            "nodeid": report.nodeid,
            "result": report.outcome
        }
        failed_case_list.append(failed_case_info)
        with open("failed_case_info.yaml", "a", encoding="utf-8") as file_obj:
            yaml.dump(failed_case_list, file_obj, allow_unicode=True)
